package br.ufrn.streamap.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.model.DualListModel;

import br.ufrn.streamap.model.dao.DAOFactory;
import br.ufrn.streamap.model.dao.HibernateDAOFactory;
import br.ufrn.streamap.model.dao.NFRDAO;
import br.ufrn.streamap.model.entity.NFR;
import br.ufrn.streamap.model.entity.NFRAttribute;
import br.ufrn.streamap.model.entity.NFRInterface;


@ManagedBean(name="selectRNF")
@SessionScoped
public class StreamAPSelectRNFs {

	private DualListModel<NFRInterface> nfrs ;
	
	
	public StreamAPSelectRNFs() {
		List<NFRInterface> source = new ArrayList<NFRInterface>();
		List<NFRInterface> target = new ArrayList<NFRInterface>();
		
		try {
			DAOFactory factory = new HibernateDAOFactory();
			NFRDAO nfrdao = factory.getNFRDAO();

			List<NFR> nList = nfrdao.findAll(NFR.class);

			for (Iterator<NFR> iterator = nList.iterator(); iterator.hasNext();) {
				NFR nfr = iterator.next();
				source.add(nfr);
				if (nfr.getAttributes() != null) {
					for (Iterator<NFRAttribute> iterator2 = nfr.getAttributes()
							.iterator(); iterator2.hasNext();) {
						NFRAttribute nfr_attr = iterator2.next();
						source.add(nfr_attr);

					}
				}

			}
		} catch (Exception e) {
			addMessage(new FacesMessage(FacesMessage.SEVERITY_ERROR,"Erro! ",e.getMessage()));
		}
		
		this.nfrs = new DualListModel<NFRInterface>(source,target);
	}

	public DualListModel<NFRInterface> getNfrs() {
		return nfrs;
	}

	public void setNfrs(DualListModel<NFRInterface> nfrs) {
		this.nfrs = nfrs;
	}
	
	public void addMessage(FacesMessage message) {  
        FacesContext.getCurrentInstance().addMessage(null, message);  
    } 
	
	
}
